'''
Created on Dec 12, 2011

@author: Box II
'''
import os
import glob
import datetime

class htmlAggregator():
    totalArray=[]
    cto=0
    lastminute=0
    lastpercent=0
    mode=1
    
    def __init__(self):
        pass  
    
    def markStart(self,mode=1):  
        now = datetime.datetime.now()
        self.lastminute=(now.minute*60)+now.second
        print("Starting at "+ str(now) + "::MSEC->"+str((now.minute*60)+now.second))
        if mode == 2:
            print("Mode set at Detailed Compilation.")
        else:
            print("Defaulted or set at Reduced data mode")
    def getDir(self,target):
        
        for currentFile in glob.glob( os.path.join(target, '*') ):
            if(os.path.isdir(currentFile)):
                now = datetime.datetime.now()
                thisPercent = self.getPct(currentFile)
                if(thisPercent>9 and thisPercent<11):
                    timeRemMins= ((now.minute*60)+now.second-self.lastminute)
                    print("SECDIFF: "+str(((now.minute*60)+now.second-self.lastminute)))
                    timeRemMins= 10 * timeRemMins
                    print("MileStone: "+str((timeRemMins/60)*4)+" Minutes estimated remaining")
                if(thisPercent>24 and thisPercent<26):
                    timeRemMins= ((now.minute*60)+now.second-self.lastminute)
                    print("SECDIFF: "+str(((now.minute*60)+now.second-self.lastminute)))
                    timeRemMins= 4 * timeRemMins
                    print("MileStone: "+str(timeRemMins/60)+" Minutes estimated remaining")
                if(thisPercent>48 and thisPercent<52):
                    timeRemMins= ((now.minute*60)+now.second-self.lastminute)
                    print("SECDIFF: "+str(((now.minute*60)+now.second-self.lastminute)))
                    print("MileStone: "+str(timeRemMins/60)+" Minutes estimated remaining")
                print str(now)+'::Evaluating Month: ' + currentFile+" Completed: %"+str(thisPercent)
                
                self.cto = self.cto+1
                if(self.cto>0):
                    self.getDir(currentFile)
                else:
                    break
                

            else:
               self.totalArray.append(self.readDetails(currentFile,self.mode))
            
    def cutFileName(self,path):
        index = path.split("\IRQ")
        rtnArray=(index[1],self.getDate(index[1]))
        return rtnArray
    
    def getPct(self,path):
        myRY= path.split('\\')
        if len(myRY) > 2:
            sigNM=((int(myRY[1])-2004)*12)+int(myRY[2])
        else:
            sigNM=((int(myRY[1])-2004)*12)
        pct=(float(sigNM)/(6.0*12.0))*100
        pct = round(pct,2)
        return pct
        
    def getDate(self,path):
        datepack=""
        datepack=datepack+""+path[0:4]+"-"+path[4:6]+"-"+path[6:8]
        return datepack
    
    def scrubHTML(self,data):  
            data=data.replace("<td>","")
            data=data.replace("</td>","")
            data=data.replace("<h3>","")
            data=data.replace("</h3>","")
            data=data.replace("</pre>","")
            data=data.replace("</code>","")
            data=data.replace("<pre>","")
            data=data.replace("<code>","")
            data=data.replace("\\n","")
            #data=data.replace(":","=>")
            #data=data.replace(",","|")
            data=data.replace("          ","")
            data=data.replace("\n","")
            if (data.find("href")>=0):
                data=data.replace("<a href","")
                data=data.replace("</a>","")
                data = data.split(">")
                if(len(data)>1):
                    data = data[1]
                else:
                    data= "ERR"
            data=data.rstrip(' ')
            data=data.lstrip(' ')
            return data
        
    def readDetails(self,path,mode):
        if mode == 1:
            return self.readDetailsRed(path)
        else:
            return self.readDetailsFull(path)
 
    def readDetailsRed(self,path):
        infile = open(path,'r')
        refPath = self.cutFileName(path)
        cto=0
        startingDet=0
        readDelay=-1
        readQuanity=0
        startTitle=0
        mode=-1
        precodes=0
        oneline=0
        details=""
        title=""
        tempArray=[]
        tempArray.append(refPath[1])
        for line in infile:
            
            if(line.find("<th>Longitude</th>")>=0):
                readDelay=3
                readQuanity=4
            if(line.find("<th>Detained</th>")>=0):
                readDelay=3
                readQuanity=4
            if(line.find("<td class='c1'>Killed in action</td>")>=0):
                readDelay=1
                readQuanity=4
            if(line.find("<td class='c1'>Wounded in action</td>")>=0):
                readDelay=1
                readQuanity=4
                
            if(readQuanity>0 and readDelay==0):
                tempArray.append(self.scrubHTML(line))
                readQuanity = readQuanity-1
                
            if(readQuanity==0):
                readDelay=-1
            if(readDelay > 0):
                readDelay = readDelay-1
            oneline=0
        return tempArray

            
    def readDetailsFull(self,path):
        infile = open(path,'r')
        refPath = self.cutFileName(path)
        cto=0
        startingDet=0
        readDelay=-1
        readQuanity=0
        startTitle=0
        mode=-1
        precodes=0
        oneline=0
        details=""
        title=""
        tempArray=[]
        tempArray.append(refPath[1])
        for line in infile:
            
            if(line.find("<th>Longitude</th>")>=0):
                readDelay=3
                readQuanity=4
            if(line.find("<th>Detained</th>")>=0):
                readDelay=3
                readQuanity=4
            if(line.find("<td class='c1'>Killed in action</td>")>=0):
                readDelay=1
                readQuanity=4
            if(line.find("<td class='c1'>Wounded in action</td>")>=0):
                readDelay=1
                readQuanity=4
            if(line.find("<div class='pane big'>")>=0):
                readDelay=1
                startTitle=1
            if(line.find("</h3>")>=0):    
                if(line.find("swap('")>=0 or line.find(">Community resources<")>=0):
                    pass
                else:
                    startTitle=0
                    title= title+self.scrubHTML(line)
                    tempArray.append("\""+title+"\"")
                
            if(line.find("<code><pre>")>=0):
                precodes= precodes+1
                startingDet=1
                if(line.find("</pre></code>")>=0):
                    oneline=1
                    startingDet=0
                    if(precodes==1):
                        tempArray.append("\""+self.scrubHTML(line)+"\"")
                    if(precodes==2):
                        pass
                        '''
                        details=self.scrubHTML(line)
                        details=details.rstrip("<br />")
                        tempSplits=details.split("<br />")
                        for datum in tempSplits:
                            splitted = datum.split(":")
                            tempArray.append(splitted[1])'''
                
                
            if(line.find("</pre></code>")>=0 and oneline==0):
                startingDet=0
                if(precodes==1):
                    tempArray.append("\""+details+"\"")
                    details=""
                if(precodes==2):
                    pass
                    '''
                    tempSplits=details.split("<br />")
                    for datum in tempSplits:
                        splitted = datum.split(":")
                        tempArray.append(splitted[1])'''
                
            if(startingDet>0):
                details = details + self.scrubHTML(line)
                
            if(startTitle>0 and readDelay==0):
                title= title+self.scrubHTML(line)
                
            if(readQuanity>0 and readDelay==0):
                tempArray.append(self.scrubHTML(line))
                readQuanity = readQuanity-1
                
            if(readQuanity==0):
                readDelay=-1
            if(readDelay > 0):
                readDelay = readDelay-1
            oneline=0
        return tempArray
                
        

        


